package com.vhall.component.controller.filter;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import com.vhall.component.controller.filter.facade.FilterWordsAdmFacade;
import com.vhall.component.entity.filter.vo.*;
import com.vhall.component.entity.page.vo.PageVO;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.enums.OperateTypeEnum;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.service.annotation.OperateLog;
import com.vhall.component.service.filter.FilterWordsAdmService;
import com.vhall.component.service.filter.constant.FilterConstant;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;


/**
 * @author longzhen.gong
 * @date 2021/7/2 16:11
 */
@VhallComponent
@Tag(name = "敏感词管理-管理端")
@TokenAuth("sys:filterword")
public class FilterWordsAdmController implements FilterWordsAdmFacade {

    private static final List<String> EXT_NAME_LIST = Arrays.asList("xlsx", "xls", "csv");

    @Autowired
    private FilterWordsAdmService filterWordsAdmService;

    /**
     * 敏感词查询-后台
     *
     * @return page
     */
    @Override
    @TokenAuth("sys:filterword:list")
    public PageVO<FilterWordsListRspVO> list(FilterWordsListReqVO reqVO) {
        Token token = TokenThreadLocal.getToken();
        if (token == null) {
            throw new BusinessException(BizErrorCode.AUTH_LOGIN_NEEDS);
        }
        return filterWordsAdmService.list(reqVO, null);
    }

    @Override
    @TokenAuth("sys:filterword:add")
    @OperateLog(type = OperateTypeEnum.CREATE, name = "保存")
    public FilterWordsCreateRspVO create(FilterWordsCreateReqVO req) {
        Token token = TokenThreadLocal.getToken();
        if (token == null) {
            throw new BusinessException(BizErrorCode.AUTH_LOGIN_NEEDS);
        }
        return filterWordsAdmService.create(req, 0, token.getAccountId());
    }

    @Override
    @TokenAuth("sys:filterword:edit")
    @OperateLog(type = OperateTypeEnum.UPDATE, name = "更新")
    public Boolean update(FilterWordsUpdateReqVO req) {
        Token token = TokenThreadLocal.getToken();
        if (token == null) {
            throw new BusinessException(BizErrorCode.AUTH_LOGIN_NEEDS);
        }
        return filterWordsAdmService.update(req);
    }

    @Override
    @TokenAuth("sys:filterword:del")
    @OperateLog(type = OperateTypeEnum.DELETE, name = "删除")
    public Integer delete(FilterWordsDeleteReqVO reqVo) {
        Token token = TokenThreadLocal.getToken();
        if (token == null) {
            throw new BusinessException(BizErrorCode.AUTH_LOGIN_NEEDS);
        }
        if (!ReUtil.isMatch(FilterConstant.REG_IDS, reqVo.getIds())) {
            throw new BusinessException(BizErrorCode.TYPE_INVALID_STRING);
        }
        return filterWordsAdmService.delete(reqVo.getIds());
    }


    /**
     * 导入敏感词
     *
     * @return
     */
    @Override
    @TokenAuth("sys:filterword:import")
    @OperateLog(type = OperateTypeEnum.IMPORT, name = "导入敏感词")
    public FilterWordsImportRspVO importFilterWords(FilterWordsAdmImportReqVO reqVo) {
        MultipartFile file = reqVo.getFilterwords();
        String fileName = file.getOriginalFilename();
        String extName = FileUtil.extName(fileName);
        if (StringUtils.isBlank(fileName)) {
            throw new BusinessException(BizErrorCode.COMP_FILTER_UPLOAD_NULL);
        }
        if (!EXT_NAME_LIST.contains(extName)) {
            throw new BusinessException(BizErrorCode.TYPE_FILE_INVALID);
        }
        Token token = TokenThreadLocal.getToken();
        if (token == null) {
            throw new BusinessException(BizErrorCode.AUTH_LOGIN_NEEDS);
        }
        Integer userId = token.getAccountId();
        return filterWordsAdmService.importFile(file, userId);
    }


}
